package com.mooc.lib_video.widget

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.customview.widget.ViewDragHelper
import com.mooc.ijk.widget.IJKView
import com.mooc.lib_video.R

//设置给ImageView用来监听滑动事件->通过检测到滑动距离压缩ImageView高度->
class AnchorScrollBehavior (context: Context,set: AttributeSet): CoordinatorLayout.Behavior<View>(context,set){
    lateinit var refView : View
    var viewGragHelper: ViewDragHelper ?= null//在zidingyiview中帮助处理手指拖拽操作->
    lateinit var context: Context
    var lastHeight = 0
    // 默认不同的回调方法区分垂直水平滑动
    override fun onLayoutChild(
        parent: CoordinatorLayout,
        child: View,
        layoutDirection: Int
    ): Boolean {
        if (viewGragHelper == null){
            //第一次进入onlayoutchild->初始化要缩放的view
            //创建drag对象
            viewGragHelper = ViewDragHelper.create(parent,1f,callBack)
            refView = child
            lastHeight = refView.measuredHeight
            context = refView.context
        }
        return super.onLayoutChild(parent, child, layoutDirection)
    }

    //处理方法
    override fun onTouchEvent(parent: CoordinatorLayout, child: View, ev: MotionEvent): Boolean {
        if (viewGragHelper == null)
            return false
        viewGragHelper!!.processTouchEvent(ev)
        return true
    }

    //拦截方法->onInterceptTouchEvent->协调者的事件拦截方法
    //touch事件分发机制->return true->父容器拦截toush事件
    //不会在向子控件分发touch事件->detail_send(linearlayout),
    //无法获取到touch事件也就无法处理对应点击监听
    //界面点击无效
    override fun onInterceptTouchEvent(
        parent: CoordinatorLayout,
        child: View,
        ev: MotionEvent
    ): Boolean {
        if (viewGragHelper == null)
            return super.onInterceptTouchEvent(parent, child, ev)
        //根据点击在屏幕中点的位置Y轴值->区分事件处理
        //当点击到底部评论输入框以及发布评论按钮位置时
        //返回false->向下分发touch事件，位置如何判断
        //获取底部linear layout布局
        val detail_player_view = parent.findViewById<IJKView>(R.id.detail_player_view)
        viewGragHelper!!.shouldInterceptTouchEvent(ev)
        if (ev.y < detail_player_view.measuredHeight)
            return true
        return false
    }

    private val callBack : ViewDragHelper.Callback = object : ViewDragHelper.Callback(){
        override fun tryCaptureView(child: View, pointerId: Int): Boolean {
            //当前的view是否可以滑动:true滑动;false
            return true
        }

        //处理垂直方向拖拽
        //child监听的view
        //top：view顶部按照y轴移动
        //dy:2次touch事件移动距离:从下向上滑动dy为负;从上向下滑动dy为正
        override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int {
            val lp : ViewGroup.LayoutParams = refView.layoutParams
            if (lp.height<0)
                lp.height = lastHeight
            lp.height =  lp.height+dy//原有高度+dy值
//            Log.e("ZXY","clampViewPositionVertical->$dy&&lp.height"+lp.height)
            //边界判断->最小高度->悬停展示高度
            //->最大高度->不能大于ImageView高度->
            //200->像素单位->无法适配
            //dp->根据屏幕密度进行转化
            if (lp.height < 200){
                lp.height = 200
            }
            if (lp.height > lastHeight) {
                lp.height = lastHeight
            }
            refView.layoutParams = lp
//            refView.requestLayout()
            return top
        }
    }

//    private fun dp2px(dpValue : Int):Int{
//        val metrics : DisplayMetrics = context.resources.displayMetrics
//        return (dpValue * metrics.density) as Int
//    }

}